package de.haw.vs.election;

import java.security.SecureRandom;

public final class Commitment implements Comparable<Commitment> {

	private final static SecureRandom RANDOM_GENERATOR = new SecureRandom();

	private final String senderUuid;
	private final int value;

	private Commitment(String uuid, int value) {
		this.senderUuid = uuid;
		this.value = value;
	}

	public int getHashValue() {
		return (senderUuid + value).hashCode();
	}

	public int getValue() {
		return value;
	}
	
	public String getSenderUuid() {
		return senderUuid;
	}
	

	/**
	 * Generiert eine neue Kommitment
	 * 
	 * @return
	 */
	public static Commitment newRandomCommitment(String uuid) {
		return new Commitment(uuid, RANDOM_GENERATOR.nextInt(Integer.MAX_VALUE));
	}

	/**
	 * Erstellt neue Kommitment
	 * 
	 * @param value
	 *            klartext
	 * @return
	 */
	public static Commitment valueOf(String uuid, int value) {
		return new Commitment(uuid, value);
	}

	@Override
	public int compareTo(Commitment o) {
		return senderUuid.compareTo(o.senderUuid);
	}

	public boolean validateHash(Integer hashValue) {
		return hashValue != null && getHashValue() == hashValue.intValue();
	}

	@Override
	public String toString() {
		return "Commitment [senderUuid=" + senderUuid + ", value=" + value + ", getHashValue()=" + getHashValue() +"]";
	}
	
	
}
